package com.lk.web.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.commons.collections.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * 用户表
 *
 * @author Aspirin
 * @since 2020-06-21
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SysUser implements UserDetails, Serializable {
  private static final long serialVersionUID = 1L;
  /** 用户 ID */
  @TableId(value = "id", type = IdType.AUTO)
  private Long id;
  /** 用户名 */
  private String username;
  /** 密码，加密存储, admin/1234 */
  private String password;
  /** 帐户是否过期(1 未过期，0已过期) 1 true 0 false */
  private boolean isAccountNonExpired = true;
  /** 帐户是否被锁定(1 未过期，0已过期) */
  private boolean isAccountNonLocked = true;
  /** 密码是否过期(1 未过期，0已过期) */
  private boolean isCredentialsNonExpired = true;
  /** 帐户是否可用(1 可用，0 删除用户) */
  private boolean isEnabled = true;
  /** 用户权限，不是sys_user的属性 */
  @TableField(exist = false)
  private Collection<? extends GrantedAuthority> authorities;

  /** 昵称 */
  private String nickName;
  /** 注册手机号 */
  private String mobile;
  /** 注册邮箱 */
  private String email;

  private LocalDateTime createDate;
  private LocalDateTime updateDate;

  /** 拥有角色集合 */
  @TableField(exist = false)
  private List<SysRole> roleList = Lists.newArrayList();
  /** 获取所有角色id */
  @TableField(exist = false)
  private List<Long> roleIds = Lists.newArrayList();

  public List<Long> getRoleIds() {
    if (CollectionUtils.isNotEmpty(roleList)) {
      roleIds = Lists.newArrayList();
      for (SysRole role : roleList) {
        roleIds.add(role.getId());
      }
    }
    return roleIds;
  }

  /** 封装当前用户拥有的权限资源对象 */
  @TableField(exist = false)
  private List<SysPermission> permissions = Lists.newArrayList();
}
